﻿<MCard>
    <MTabs @bind-Value="_tabValue"
           BackgroundColor="red lighten-2"
           Dark>
        @foreach (var tab in _tabs)
        {
            <MTab @key="tab" Value="tab">
                <span>
                    Tab @tab
                </span>
                <MButton Icon OnClick="@(() => RemoveTab(tab))"
                         OnClickStopPropagation>
                    <MIcon>mdi-close</MIcon>
                </MButton>
            </MTab>
        }
        <MButton Icon Large Class="my-auto"
                 OnClick="AddNewTab">
            <MIcon>mdi-plus</MIcon>
        </MButton>
    </MTabs>

    <MCardText Class="text-center">
        <MTabsItems Value="@_tabValue">
            @foreach (var tab in _tabs)
            {
            <MTabItem @key="tab" Value="tab">
                Tab @tab
            </MTabItem>
            }
        </MTabsItems>
    </MCardText>
</MCard>

@code {
    private StringNumber _tabValue;

    private List<int> _tabs = Enumerable.Range(1, 3).ToList();

    private void AddNewTab()
    {
        var newTab = _tabs.Count > 0 ? _tabs.Max() + 1 : 1;
        _tabs.Add(newTab);
        _tabValue = newTab;
    }

    private void RemoveTab(int val)
    {
        _tabs.Remove(val);

        if (_tabValue == val)
        {
            _tabValue = _tabs.LastOrDefault();
        }
    }
}